package com.untrance.shop.core.builder.query;

import javax.persistence.EntityManager;

import com.untrance.shop.common.enums.OrderDirectionType;
import com.untrance.shop.common.filters.model.ProductsCatalogFilter;
import com.untrance.shop.model.Product;

/**
 * @author eugene zadyra
 */
public class ProductsCatalogQueryBuilder extends AbstractPaginableQueryBuilder<ProductsCatalogFilter> implements
		QueryBuilder {

	public ProductsCatalogQueryBuilder(ProductsCatalogFilter filter, EntityManager em) {
		this.em = em;
		this.filter = filter;
		addFilters();
	}

	@Override
	protected void addFilters() {
		addFilterValue("categoryKey", filter.getCategoryIdList());
	}

	@Override
	protected OrderDirectionType getDefaultOrderDirection() {
		return null;
	}

	@Override
	protected String getEntityName() {
		return Product.class.getName();
	}

	@Override
	protected String getOrderField() {
		if (filter.getOrderBy() != null) {
			switch (filter.getOrderBy()) {
			case BY_ALPHABET:
				return "e.name";
			case BY_DATE:
				return "e.creationDate";
			case BY_PRICE:
				return "e.oldPrice";
			}
		}
		return null;
	}
}
